﻿/* 
 * Since 2008 - 2012
 */

package com.hh.bss.project.service.impl;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import javax.servlet.http.HttpServletResponse;

import com.hh.bss.base.*;
import com.hh.bss.common.MapObjectRowMapper;
import com.hh.bss.common.exception.RepeatRecordException;
import com.hh.bss.common.page.*;
import com.hh.bss.common.page.impl.*;
import com.hh.bss.customer.entity.Company;
import com.hh.bss.customer.entity.CompanyCv;
import com.hh.bss.customer.service.CompanyService;
import com.hh.bss.customer.vo.query.CompanyQuery;
import com.hh.bss.project.dao.*;
import com.hh.bss.project.entity.*;
import com.hh.bss.project.service.*;
import com.hh.bss.project.vo.query.*;
import com.hh.bss.talent.entity.Cv;
import com.hh.bss.talent.service.CvService;
import com.hh.bss.talent.vo.query.CvQuery;
import com.hh.bss.util.*;




@Service
@Transactional
public class SearchProjectServiceImpl extends BaseBusinessService<SearchProject,java.lang.Integer>  implements SearchProjectService<SearchProject,java.lang.Integer>{

	private CvService cvService;
	private CompanyService companyService;
	private SearchProjectCvService searchProjectCvService;
	public void setCvServiceImpl(CvService service) {
		this.cvService = service;
	}
	public void setSearchProjectCvServiceImpl(SearchProjectCvService service) {
		this.searchProjectCvService = service;
	}
	public void setCompanyServiceImpl(CompanyService service) {
		this.companyService = service;
	}
	private SearchProjectDao searchProjectDao;
	/**增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,请注意大小写*/
	public void setSearchProjectDaoImpl(SearchProjectDao dao) {
		this.searchProjectDao = dao;
	}
	public EntityDao getEntityDao() {
		return this.searchProjectDao;
	}
	
	@Transactional(readOnly=true)
	public Page findPage(SearchProjectQuery query) {
		return searchProjectDao.findPage(query);
	}
	public Page findProjectPage(SearchProjectQuery query) {
		return searchProjectDao.findProjectPage(query);
	}
	public void save(SearchProject searchProject){
		
		StringBuffer sql=new StringBuffer();
		String[] industrys=searchProject.getIndustrys().split(",");
		String[] functions=searchProject.getFunctions().split(",");
		sql.append("select * from ( select t.name,t.industrys,t.functions from   search_project t where 1=1 ");
		List values=new ArrayList();
		if (industrys!=null&&industrys.length>0){
			sql.append(" and (");
			for(int i=0;i<industrys.length;i++){
				if (i==0)
				  sql.append(" concat(',',t.industrys,',') like ?  ");
				else
					sql.append(" and concat(',',t.industrys,',') like ?  ");	
				values.add("%"+industrys[i]+"%");
			}
			sql.append(" )");
		}
		if (functions!=null&&functions.length>0){
			sql.append(" and (");
			for(int i=0;i<functions.length;i++){
				if (i==0)
				  sql.append(" concat(',',t.functions,',') like ?  ");
				else
					sql.append(" and concat(',',t.functions,',') like ?  ");	
				values.add("%"+functions[i]+"%");
			}
			sql.append(" )");
		}
		sql.append(" ) p ");

		List l=this.getEntityDao().getJdbcDao().findByQuery(sql.toString(), values.toArray(), new MapObjectRowMapper());
		for (int i=0;l!=null&&i<l.size();i++){
			Map m=(Map)l.get(i);
			boolean ie=false;
			boolean fe=false;
			String ccname=(String)m.get("name");
			String mi=(String)m.get("industrys");
			String mf=(String)m.get("functions");
			if (!StringHelper.isEmpty(mi)){
				String[] aa=mi.split(",");
				if (aa!=null&&industrys!=null&&aa.length==industrys.length) ie=true;
			}
			if (!StringHelper.isEmpty(mf)){
				String[] bb=mf.split(",");
				if (bb!=null&&functions!=null&&bb.length==functions.length) fe=true;
			}
			//if(ie&&fe) throw new RepeatRecordException("["+ccname+"]存在相同行业和职能的CC项目");
		}
		
		searchProjectDao.save(searchProject);
	}
	
	
	/**
	 * 
	 */
	public int  checkSearchList(String industrys, String functions) {
		List  values = new ArrayList ();
		StringBuffer sql = new StringBuffer();
		sql.append("select count(1) as  count from search_project where industrys = '").append(industrys);
		sql.append("' and functions = '").append(functions).append("' ");
		int count = this.getEntityDao().getJdbcDao().getJdbcTemplate().queryForInt(sql.toString());
		return count;
	}
	public void update(SearchProject searchProject){
		StringBuffer sql=new StringBuffer();
		String[] industrys=searchProject.getIndustrys().split(",");
		String[] functions=searchProject.getFunctions().split(",");
		sql.append("select * from ( select t.id,t.industrys,t.functions from   search_project t where 1=1 ");
		List values=new ArrayList();
		if (industrys!=null&&industrys.length>0){
			sql.append(" and (");
			for(int i=0;i<industrys.length;i++){
				if (i==0)
				  sql.append(" concat(',',t.industrys,',') like ?  ");
				else
					sql.append(" and concat(',',t.industrys,',') like ?  ");	
				values.add("%"+industrys[i]+"%");
			}
			sql.append(" )");
		}
		if (functions!=null&&functions.length>0){
			sql.append(" and (");
			for(int i=0;i<functions.length;i++){
				if (i==0)
				  sql.append(" concat(',',t.functions,',') like ?  ");
				else
					sql.append(" and concat(',',t.functions,',') like ?  ");	
				values.add("%"+functions[i]+"%");
			}
			sql.append(" )");
		}
		sql.append(" ) p ");

		List l=this.getEntityDao().getJdbcDao().findByQuery(sql.toString(), values.toArray(), new MapObjectRowMapper());
		for (int i=0;l!=null&&i<l.size();i++){
			Map m=(Map)l.get(i);
			boolean ie=false;
			boolean fe=false;
			String ccname=(String)m.get("name");
			String mi=(String)m.get("industrys");
			String mf=(String)m.get("functions");
			Integer pid=(Integer)m.get("id");
			if (!StringHelper.isEmpty(mi)){
				String[] aa=mi.split(",");
				if (aa!=null&&industrys!=null&&aa.length==industrys.length) ie=true;
			}
			if (!StringHelper.isEmpty(mf)){
				String[] bb=mf.split(",");
				if (bb!=null&&functions!=null&&bb.length==functions.length) fe=true;
			}
			
			//if(ie&&fe&&!searchProject.getId().equals(pid)) throw new RepeatRecordException("["+ccname+"]存在相同行业和职能的CC项目");
		}
		
		searchProjectDao.update(searchProject);
	}

	public boolean cvJoinProject(SearchProjectCv searchProjectCv) {
		// TODO Auto-generated method stub
		System.out.println(searchProjectCv.getProjectId()+"=cvJoinProject="+searchProjectCv.getCompanyId());
		if (searchProjectCv.getProjectId()==null||searchProjectCv.getCvId()==null) return false;
		//查询人才
		Cv cv=cvService.getById(searchProjectCv.getCvId());
		//检查是否受保护(暂时不做保护)
		//if (cvService.isProtectCv(searchProjectCv.getCvId()))  throw new RuntimeException("人选["+cv.getCvName()+"]受到保护，无法添加为目标人选。"); 
		
		Object[] os={searchProjectCv.getProjectId(),searchProjectCv.getCvId()};
		List l=searchProjectDao.getHibernateTemplate().find("from SearchProjectCv  pc where pc.projectId=? and pc.cvId=? ", os);
		
		if (l!=null&&l.size()>0){
			//已经存在
			return true;
		}
		searchProjectDao.getHibernateTemplate().save(searchProjectCv);
		return true;
	}

	public boolean companyJoinProject(SearchProjectCompany searchProjectCompany) {
		// TODO Auto-generated method stub
		//
		
		
		
		if (searchProjectCompany.getProjectId()==null||searchProjectCompany.getCompanyId()==null) return false;
		
		//查询公司
		Company company=(Company)companyService.getById(searchProjectCompany.getCompanyId());
		//检查是否受保护(暂时不做保护)
		//if (companyService.isProtect(searchProjectCompany.getCompanyId()))  throw new RuntimeException("公司["+company.getName()+"]受到保护，无法添加为目标公司。"); 
		
		Object[] os={searchProjectCompany.getProjectId(),searchProjectCompany.getCompanyId()};
		List l=searchProjectDao.getHibernateTemplate().find("from SearchProjectCompany  pc where pc.projectId=? and pc.companyId=? ", os);
		if (l!=null&&l.size()>0){
			//已经存在
			return true;
		}
		searchProjectDao.save(searchProjectCompany);
		searchProjectDao.flush();
		
		//自动更新cc项目人选
		
		searchProjectCvService.addProjectCvByCompanyCv(searchProjectCompany.getCompanyId());
		/*
		//加入其下面的cv
		//List notExistList=searchProjectDao.getHibernateTemplate().find("from SearchProjectCv  ccv where ccv.cvId not in (select pcv.cvId from SearchProjectCv pcv where pcv.projectId =? ) ", searchProjectCompany.getProjectId());
		List values2 = new ArrayList();
		values2.add(searchProjectCompany.getCompanyId());
		values2.add(searchProjectCompany.getProjectId());
		values2.add(searchProjectCompany.getProjectId());
		StringBuffer sql=new StringBuffer();
		sql.append(" select a.cv_id,a.dept_id,a.title_id from company_cv a,cv c  where a.company_id=? and a.cv_id=c.id and not exists (select b.cv_id from search_project_cv b where b.cv_id=a.cv_id and b.project_id=?) ");
		sql.append(" and exists(select 1 from search_project d where d.id=? and find_in_set(c.function,d.functions)>0 )");
		List notExistList=this.getEntityDao().getJdbcDao().findByQuery(sql.toString(), values2.toArray(), new MapObjectRowMapper());
		SearchProjectCv searchProjectCv=null;
		Map companyCv=null;
		System.out.println("*****************companyCv notExistList:"+notExistList);
		for(int i=0;notExistList!=null&&i<notExistList.size();i++){
			searchProjectCv=new SearchProjectCv();			
			companyCv=(Map)notExistList.get(i);
			System.out.println("*****************companyCv:"+companyCv);
			searchProjectCv.setCreateDt(new Date());
			searchProjectCv.setCreator(searchProjectCompany.getCreator());
			searchProjectCv.setProjectId(searchProjectCompany.getProjectId());
			searchProjectCv.setCompanyId(searchProjectCompany.getCompanyId());
			searchProjectCv.setCvId((Integer)companyCv.get("cv_id"));
			searchProjectCv.setDeptId((Integer)companyCv.get("dept_id"));
			searchProjectCv.setTitleId((Integer)companyCv.get("title_id"));
			searchProjectDao.getHibernateTemplate().save(searchProjectCv);
		}
	   	*/
		
		return true;
	}
	
	public boolean jobJoinProject(ContractJobSearchProject contractJobSearchProject) {
		// TODO Auto-generated method stub
		//
		//System.out.println(contractJobSearchProject.getProjectId()+"=cvJoinProject="+searchProjectCv.getCompanyId());
		if (contractJobSearchProject.getProjectId()==null||contractJobSearchProject.getJobId()==null) return false;
		
		Object[] os={contractJobSearchProject.getProjectId(),contractJobSearchProject.getJobId()};
		List l=searchProjectDao.getHibernateTemplate().find("from ContractJobSearchProject  cjs where cjs.projectId=? and cjs.jobId=? ", os);
		
		if (l!=null&&l.size()>0){
			//已经存在
			return true;
		}
		searchProjectDao.getHibernateTemplate().save(contractJobSearchProject);
		return true;
	}
	
	public Page findContactRecordPage(ContactRecordQuery contactRecordQuery) {
		// TODO Auto-generated method stub
		return searchProjectDao.findContactRecordPage(contactRecordQuery);
	}
	@Override
	public void exportSearchRpeport(SearchRpeportQuery query, HttpServletResponse response) {
		searchProjectDao.exportSearchRpeport(query,response);
		
	}
	@Override
	public boolean checkCompanyIntegrity(Integer companyid) {
		return companyService.checkCompanyIntegrity(companyid);
	}

	
	
	
}
